package com.lottery.ruok.lottrey.userInfo;

import android.content.SharedPreferences;
import android.net.Uri;

import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomAdminResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomUsersResponse;
import com.lottery.ruok.lottrey.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

import property.SharePreferenceProperties;
import utils.StringUtils;
import utils.Utils;

public class UserInfo {
    private final static String FILE_NAME = "user_config";
    private static final String START_POLL_TIME = "start_activities_poll_time";
    private static SharedPreferences sp;
    private static String admin = "";
    private static int chatroom_role = 0;
    private static int nonet = 0;
    private static int nextDomain = 0;
    private static GetRoomUsersResponse roomInfo;
    public static List<String> mDomain = new ArrayList<>();
    public static List<GetRoomAdminResponse.ListBean> roomManagers = new ArrayList<>();
    private static String roomName="";

    public static int getChatroom_role() {
    return     getInstance().getInt("chatroom_role", 0);
    }

    public static void setChatroom_role(int chatroom_role) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("chatroom_role", chatroom_role);
        edit.commit();
    }

    private static SharedPreferences getInstance() {
        if (sp == null) {
            sp = Common.getIns().getContext().getSharedPreferences(FILE_NAME, 0);
        }
        return sp;
    }

    /**
     * 用户名称
     *
     * @return
     */
    public static String getUsername() {
        return getInstance().getString("name", "");
    }

    public static void setUsername(String name) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("name", name);
        edit.commit();
        SharePreferenceProperties.set("name", name);
    }

    public static int getEnableWithdrawal() {
        return getInstance().getInt("enable_withdrawal", 1);
    }

    public static void setEnableWithdrawal(int enableWithdrawal) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("enable_withdrawal", enableWithdrawal);
        edit.commit();
    }

    /**
     * 用户昵称
     *
     * @return
     */
    public static String getNickname() {
        return getInstance().getString("nickname", "");
    }

    public static void setNickname(String name) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("nickname", name);
        edit.commit();
    }
    /**
     * 用户手机号
     *
     * @return
     */
    public static String getMobilePhone() {
        return getInstance().getString("mobile_phone", "");
    }

    public static void setMobilePhone(String name) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("mobile_phone", name);
        edit.commit();
        SharePreferenceProperties.set("mobile_phone", name);
    }
    /**
     * 用户Id
     *
     * @return
     */
    public static String getUserId() {
        return getInstance().getString("user_id", "");
    }

    public static void setUserId(String name) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("user_id", name);
        edit.commit();
        SharePreferenceProperties.set("user_id", name);
    }
    /**
     * 默认彩票
     *
     * @return
     */
    public static String getDefaultLottery() {
        return getInstance().getString("default_Lottery", "");
    }

    public static void setDefaultLottery(String name) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("default_Lottery", name);
        edit.commit();
        SharePreferenceProperties.set("mobile_phone", name);
    }
    public static String getToken() {
        return getInstance().getString("token", "");
    }

    public static void setToken(String token) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("token", token);
        edit.commit();
    }

    public static String getUniqueId() {
        return getInstance().getString("unique_id", "");
    }

    public static void setAccount(String s) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("account", s);
        edit.commit();
    }

    public static String getAccount() {
        return getInstance().getString("account", "0.00");
    }

    public static void setIsTest(int s) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("is_test", s);
        edit.commit();
    }
//试玩账号
    public static int getIsTest() {
        return getInstance().getInt("is_test", 0);
    }

    public static void setIsTry(int isTry) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("is_try", isTry);
        edit.commit();
    }



    public static int getIsTry() {
        return getInstance().getInt("is_try", 0);
    }

    public static void setFingerLock(boolean s) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putBoolean("lock", s);
        edit.commit();
    }

    public static boolean getFingerLock() {
        return getInstance().getBoolean("lock", false);
    }

    public static void setAvatar(String s) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("avatar", s);
        edit.commit();
    }

    public static String getAvatar() {
        return getInstance().getString("avatar", "");
    }

    public static String getRoomId() {
        return getInstance().getString("rong_room_id", "");
    }

    public static void setRoomId(String r) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("rong_room_id", r);
        edit.commit();
    }
//是否是管理员
    public static String getAdmin() {
        return admin;
    }

    public static void setAdmin(String admin) {
        UserInfo.admin = admin;
    }
    public static String getRoomName() {
        return  getInstance().getString("room_name", "");
    }

    public static void setRoomName(String roomNamemin) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("room_name", roomNamemin);
        edit.commit();
    }
    public static int getRoomNumCount() {
        return  getInstance().getInt("room_num_count", 0);
    }

    public static void setRoomNumCount(int roomNamemin) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("room_num_count",roomNamemin);
        edit.commit();
    }
    /**
     * 记录是否是第一次启动
     *
     * @return
     */
    public static boolean getFirstStart() {
        return getInstance().getBoolean("firstStart", true);
    }

    public static void setFirstStart(boolean flag) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putBoolean("firstStart", flag);
        edit.commit();
    }

    public static int getVersionCode() {
        return getInstance().getInt("versioncode", 0);
    }

    public static void setVersionCode(int flag) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("versioncode", flag);
        edit.commit();
    }
    public static void setStandbyURL(String urlCombined){
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("standbyurl", urlCombined);
        edit.commit();
    }
    public static String getStandbyURL(){
        return getInstance().getString("standbyurl", "");
    }

    public static int getNoNet() {
//        return getInstance().getInt("nonet", 0);
        return nonet;
    }

    public static void setNoNet(int cnt) {
//        SharedPreferences.Editor edit = getInstance().edit();
//        edit.putInt("nonet", cnt);
//        edit.commit();
        nonet = cnt;
    }

//    public static void setLocalUrl(String s){
//        SharedPreferences.Editor edit = getInstance().edit();
//        edit.putString("localUrl", s);
//        edit.commit();
//    }
//
//    public static String getLocalUrl(){
//        return getInstance().getString("localUrl", "");
//    }

    public static void setQuickDomain(String s) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("quickDomain", s);
        edit.commit();
    }

    public static String getQuickDomain() {
//        return getInstance().getString("quickDomain", "");
        return Common.APP_URL;
    }

    public static void setQuickUrl(String s) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("quickUrl", s);
        edit.commit();
    }

    public static String getQuickUrl() {
        return getInstance().getString("quickUrl", "");
    }

    // 從 SharedPreferences 折解之前合併的所有備用 URL, 隨機挑一個
//    public static String getNextURL(){
//        String urlCombined = getStandbyURL();
//        ArrayList<String> arrUrl = new ArrayList<>();
//        String str = urlCombined.replace("http", "^http");
//        StringTokenizer st = new StringTokenizer(str, "^");
//        while(st.hasMoreTokens()) {
//            arrUrl.add(st.nextToken());
//        }
//        if(arrUrl.size() == 0){
//            return Common.APP_URL;
//        }
//        int index = new Random().nextInt(arrUrl.size());
//        if(index < arrUrl.size()){
//            return arrUrl.get(index);
//        } else {
//            return Common.APP_URL;
//        }
//    }

    /**
     * 没有元角分
     *
     * @param quickAmount
     */
    public static void setQuickAmount(String quickAmount) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("new_quick_amount", quickAmount);
        edit.commit();
    }

    public static String getQuickAmount() {
        return getInstance().getString("new_quick_amount", "1;3;8;24;72");
    }

    /**
     * 有元角分
     *
     * @param quickAmount
     */
    public static void setQuickAmountYJF(String quickAmount) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("new_quick_amount_yjf", quickAmount);
        edit.commit();
    }

    public static String getQuickAmountYJF() {
        return getInstance().getString("new_quick_amount_yjf", "1;3;8;24;72");
    }

    public static void setQuickAmountUnit(String unit) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("quick_amount_unit", unit);
        edit.commit();
    }

    public static String getQuickAmountUnit() {
        return getInstance().getString("quick_amount_unit", "元");
    }

    public static void setShowTip(boolean tip) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putBoolean("setShowTip", tip);
        edit.commit();
    }
    //是否被踢出群
    public static void setChatroom_block(int chatroom_block) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("chatroom_block", chatroom_block);
        edit.commit();
    }

    public static int getChatroom_block() {
        return getInstance().getInt("chatroom_block", 1);

    }

    public static boolean getShowTip() {
        return getInstance().getBoolean("setShowTip", true);
    }

    public static void setRoomAdminInfo(List<GetRoomAdminResponse.ListBean> list) {
        roomManagers = list;
    }

    public static GetRoomUsersResponse getRoomInfo() {
        return roomInfo;
    }

    /**
     * 判断是否是管理员
     *
     * @param username
     * @return
     */
    public static boolean checkIsManager(String username) {
        if (Utils.isEmpty(username)) {
            return false;
        }
        if (roomManagers != null) {
            for (int i = 0; i < roomManagers.size(); i++) {
                GetRoomAdminResponse.ListBean user = roomManagers.get(i);
                if (username.equals(user.getNickname())) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void setChatStatus(int status) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putInt("chatroom_status", status);
        edit.commit();
    }

    public static int getChatStatus() {
        return getInstance().getInt("chatroom_status", 1);

    }

    public static void setRongCloudToken(String rongCloudToken) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("rongcloud_token", rongCloudToken);
        edit.commit();
    }

    public static String getRongCloudToken() {
        return getInstance().getString("rongcloud_token", "");

    }
    public static void setMyUserId(String rongCloudToken) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("my_user_id", rongCloudToken);
        edit.commit();
    }

    public static String getMyUserId() {
        return getInstance().getString("my_user_id", "");

    }
    public static io.rong.imlib.model.UserInfo getRongCloudUserInfo() {
        if (StringUtils.isEmpty(getToken())) {
            //没有登录
            return null;
        }
        return new io.rong.imlib.model.UserInfo(getRongCloudUserId(), getUsername(), Uri.parse(getAvatar()));
    }

    private static void setRongCloudUserId(String rongCloudUserId) {
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putString("rongcloud_userId", rongCloudUserId);
        edit.commit();
    }

    private static String getRongCloudUserId() {
        return getInstance().getString("rongcloud_userId", "");
    }
    public static synchronized void saveIP(String ip) {
        mDomain.add(ip);
    }

    public static synchronized void replaceDomain() {
        if (mDomain.size() > 0) {
            if (nextDomain == mDomain.size()) {
                nextDomain = 0;
            }
            SharePreferenceProperties.set(ConstantUtil.DOWNLOAD_IP, mDomain.get(nextDomain));
            RetrofitBase.resetRetrofit();
            nextDomain++;
        }
    }

    /**
     * 活动奖励轮询查询时间
     * @return
     */
    public static long getStartActivitiesPollTime(){
        return getInstance().getLong(START_POLL_TIME,0);
    }
    public static void setStartActivitiesPollTime(long timestamp){
        SharedPreferences.Editor edit = getInstance().edit();
        edit.putLong(START_POLL_TIME, timestamp/1000).apply();
    }
}